package com.ruike.alisurface.http;

import com.ruike.alisurface.bean.CarGoodsBeans;
import com.ruike.alisurface.bean.CardCreateOrderByProductBean;
import com.ruike.alisurface.bean.CardCreateOrderBySlotBean;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.ApiTools;
import com.voodoo.lib_frame.httpTools.RequestParams;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: voodoo
 * CreateDate: 2020-03-26 026 下午 01:59
 * Description: 卡的网络请求类
 */
public class CardHttp {

    // 根据商品创建订单  POST
    public static final String CREATE_ORDER_BY_PRODUCT = "https://rk.shouhuojiyun.com/otherapi/Xiechuangzhitong/create_order_by_product";
    // 根据货道创建订单  POST
    public static final String CREATE_ORDER_BY_SLOT = "https://rk.shouhuojiyun.com/otherapi/Xiechuangzhitong/create_order_by_slot";
    // 卡支付  GET
    public static final String CARD_PAY = "https://rk.shouhuojiyun.com/otherapi/Xiechuangzhitong/card_pay";
    // 上报订单出货情况  GET
    public static final String END_ORDER = "https://rk.shouhuojiyun.com/otherapi/Xiechuangzhitong/end_order";

    /**
     * 按照商品创建订单
     *
     * @param carGoodsBeansList 购物车商品列表
     * @param listener          回调
     */
    public static void createOrderByProduct(List<CarGoodsBeans> carGoodsBeansList, BaseView listener) {

        CardCreateOrderByProductBean cardCreateOrderByProductBean = new CardCreateOrderByProductBean();
        cardCreateOrderByProductBean.setMchid(ShareUtils.getInstance().getString(ShareKey.MCH_ID));

        List<CardCreateOrderByProductBean.DetailsBean> detailsBeanList = new ArrayList<>();
        for (CarGoodsBeans cardGoodsBean : carGoodsBeansList) {
            String carGoodsProductId = cardGoodsBean.getProductid();

            // 先将对象定义出来
            CardCreateOrderByProductBean.DetailsBean detailsBean = null;
            // 轮询列表，查看是否有此对象，有的将数据加1并设置进去
            for (int i = 0; i < detailsBeanList.size(); i++) {
                CardCreateOrderByProductBean.DetailsBean myDetailsBean = detailsBeanList.get(i);
                String productId = myDetailsBean.getProductid();
                if (productId.equals(carGoodsProductId)) {
                    detailsBean = myDetailsBean;
                    detailsBean.setQuantity(detailsBean.getQuantity() + 1);
                    detailsBeanList.set(i, detailsBean);
                    break;
                }
            }
            // 判断是否给对象赋值，如果没赋值，说明列表中没有添加过，直接添加进去
            if (detailsBean == null) {
                detailsBean = new CardCreateOrderByProductBean.DetailsBean();
                detailsBean.setQuantity(1);
                detailsBean.setProductid(carGoodsProductId);

                // 将购物车中商品对应的货道列表字符串转换成 ArrayList 列表
                String slotIndexStr = cardGoodsBean.getSlotIndexs();
                List<Integer> slotList = new ArrayList<>();
                try {
                    JSONArray jsonArray = new JSONArray(slotIndexStr);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        slotList.add(Integer.parseInt(jsonArray.getString(i)));
                    }
                } catch (JSONException | NumberFormatException e) {
                }

                detailsBean.setSlotIndexs(slotList);
                detailsBeanList.add(detailsBean);
            }

        }
        cardCreateOrderByProductBean.setDetails(detailsBeanList);

        new ApiTools().postJsonRequest(CREATE_ORDER_BY_PRODUCT, GsonUtils.model2Json(cardCreateOrderByProductBean), listener);
    }

    /**
     * 按照货道创建订单
     *
     * @param carGoodsBeansList 购物车商品列表
     * @param listener          回调
     */
    public static void createOrderBySlot(List<CarGoodsBeans> carGoodsBeansList, BaseView listener) {

        CardCreateOrderBySlotBean cardCreateOrderBySlotBean = new CardCreateOrderBySlotBean();
        cardCreateOrderBySlotBean.setMchid(ShareUtils.getInstance().getString(ShareKey.MCH_ID));

        List<CardCreateOrderBySlotBean.DetailsBean> detailsBeanList = new ArrayList<>();

        for (CarGoodsBeans cardGoodsBean : carGoodsBeansList) {
            String slotIndexStr = cardGoodsBean.getSlotIndexs();
            int slotIndex = -1;
            try {
                slotIndex = Integer.parseInt(slotIndexStr);
            } catch (NumberFormatException e) {
            }

            CardCreateOrderBySlotBean.DetailsBean detailsBean = null;
            for (int i = 0; i < detailsBeanList.size(); i++) {
                CardCreateOrderBySlotBean.DetailsBean myDetailsBean = detailsBeanList.get(i);
                if (myDetailsBean.getSlotIndex() == slotIndex) {
                    detailsBean = myDetailsBean;
                    detailsBean.setQuantity(detailsBean.getQuantity() + 1);
                    detailsBeanList.set(i, detailsBean);
                    break;
                }
            }

            if (detailsBean == null) {
                detailsBean = new CardCreateOrderBySlotBean.DetailsBean();
                detailsBean.setQuantity(1);
                detailsBean.setSlotIndex(slotIndex);
                detailsBeanList.add(detailsBean);
            }

        }

        cardCreateOrderBySlotBean.setDetails(detailsBeanList);

        new ApiTools().postJsonRequest(CREATE_ORDER_BY_SLOT, GsonUtils.model2Json(cardCreateOrderBySlotBean), listener);

    }

    /**
     * 按照货道创建订单
     *
     * @param orderId  创建的订单ID
     * @param cardNo   十位十进制卡号
     * @param remark   备注信息
     * @param listener 回调
     */
    public static void cardPay(String orderId, String cardNo, String remark, BaseView listener) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        params.put("order_id", orderId);
        params.put("card_no", cardNo);
        params.put("remark", remark);
        new ApiTools().getRequest(CARD_PAY, params, listener);
    }

    /**
     * 上报出货情况
     *
     * @param orderId  创建的订单ID
     * @param isEnd    是否完成
     * @param remark   备注信息
     * @param listener 回调
     */
    public static void endOrder(String orderId, boolean isEnd, String remark, BaseView listener) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        params.put("order_id", orderId);
        params.put("IsEnd", isEnd);
        params.put("remark", remark);
        new ApiTools().getRequest(END_ORDER, params, listener);
    }

}